package com.platform.core.sms;

//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

@Log4j2
public class SmsDemoCode {
    private static final String CONTENT_TYPE = "Content-Type";
    private static final String JSON_TYPE = "application/json;charset=UTF-8";
    private static final String C_TENANCY_ID = "C-Tenancy-Id";
    private static final String C_BUSINESS_ID = "C-Business-Id";
    private static final String C_APP_ID = "C-App-Id";
    private static final String REFERER = "referer";
    private static final String REQ_TOKEN_FLAG = "C-Dynamic-Password-Foruser";


    private static final String SMS_API = "/gsp/fsx06033";

    /**
     * 短信服务地址
     */
    private static final String URL = "http://103.126.126.119:8039";
    /**
     * 签名私钥
     */
    private static final String PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDpjaF2eGZnMp1MewcYpPVMo8aQxLloZNaTz2lNegZvYWexhoOsbWJ58VEB1VOxiFcJhToA790wDwCBFdGsNlqIF8pBK4WCNjCS065us2Ryygf8B57ePiNbw0UmAFablExOw4uu6coLayyBSBUpTwFz29pIQ8NINEQy7HmOahKVjRYOYNWGc51lLhavqRXk6AIjFi4hLYTrT2CSRH/yVsSYO7D6DwAwdfK2xDJcIgjoVavtKvp+HbhAdr4e5XvykHjttmQzsSfCUgRcvVhTiJXWcEly1dYbQI4Yin9WvrWLmnuhSBLMqPIh4l9qq3z7AUXgUfspY4VqdJKFtTXNgmzhAgMBAAECggEBAI+H7vazv5skS+84nH/UwLu82scVXyx87DIigYCPwWyp2+QJ2toYzzBJ0ZLi23B7baH4MlFpF/H1BkZOdnDzZmfE1xN9/6qqSuexF74ImW7CIm4M8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erFZ+SM2RRzQ8pilc4yNulXYZ7QV8=";
    /**
     * 签名算法
     */
    private static final String SGN_ALGORITHM = "SHA256withRSA";

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add(CONTENT_TYPE, JSON_TYPE);
        headers.add(C_TENANCY_ID, "110000000000");
        headers.add(C_BUSINESS_ID, "20181115124830231010100100010010");
        headers.add(C_APP_ID, "GSP_APP_001");
        headers.add(REFERER, URL);
        headers.add(REQ_TOKEN_FLAG, "387262e76a3147618b74524ca6ab2fa3");


        Map<String, String> messageBo = Maps.newHashMap();
        messageBo.put("MblPh_No", "18115265929");
        messageBo.put("Rmndr_Cntnt", "测试指定用户推送");

        List<Map<String, String>> list = Lists.newArrayList();
        list.add(messageBo);

        String zrStr = ""; // siteMessage(list);
        log.info(zrStr);

        HttpEntity<String> entity = new HttpEntity<>(zrStr, headers);
        charsetUtf8(restTemplate);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(URL + SMS_API, entity, String.class);
        String responseEntityBody = stringResponseEntity.getBody();
        log.info("responseEntityBody: " + responseEntityBody);

    }

   /* public static String siteMessage(List<Map<String, String>> list) {
        //循环外字段
        JSONObject json = new JSONObject();
        //版本号 固定值2
        json.put("VNo", "2");
        //签名算法
        json.put("Sgn_Algr", SGN_ALGORITHM);
        //推送范围标识 1-全部推送,2-指定用户推送
        json.put("Psh_Scop_Idr", "2");
        //批次号 -> 租户下全局唯一
        json.put("BtNo", random(4));
        JSONArray rmndrArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonStr = new JSONObject();
            //序号从1开始
            jsonStr.put("SN", (i + 1) + "");
            String phone = list.get(i).get("MblPh_No");
            String content = list.get(i).get("Rmndr_Cntnt");
            //手机号
            jsonStr.put("MblPh_No", phone);
            //用户id
            jsonStr.put("Usr_ID", phone);
            //提醒内容
            jsonStr.put("Rmndr_Cntnt", content);
            //提醒种类 00-短信,  -01-App,  02-短信+App，03-站内信
            jsonStr.put("Rmndr_CtCd", "00");
            rmndrArray.add(jsonStr);
        }
        //循环域
        json.put("LIST1", rmndrArray);
        Set<String> set = new HashSet<>();
        set.add("LIST1");
        //拼接原串信息
        String signStr = AlgorithmOriSplicingUtil.createSign(json.toString(), set);
        //政融签名 -> 私钥签名 -> 用于双方认证
        String signInf = SHA256withRSA.sign(PRIVATE_KEY, signStr);
        //签名信息
        json.put("SIGN_INF", signInf);
        return json.toString();
    }
*/
    /**
     * 生成批次号 -> 租户下全局唯一标识
     *
     * @param length 长度
     * @return 14位字符串
     */
    private static String random(int length) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = sdf.format(new Date());
        StringBuilder sb = new StringBuilder();
        String s = "0123456789";
        for (int i = 0; i < length; i++) {
            Random rand = new Random();
            int num = rand.nextInt(10);
            sb.append(s.charAt(num));
        }
        return date + sb;
    }

    private static void charsetUtf8(RestTemplate restTemplate) {
        restTemplate.getMessageConverters().forEach(httpMessageConverter -> {
            if (httpMessageConverter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(StandardCharsets.UTF_8);
            }
        });
    }
}
